package Bean.Log;

import Action.Manager.AddUser;
import Action.AltUser;
import Action.Sys.Setting;
import Action.Sys.Login;
import Bean.UserinfoEntity;
import org.apache.struts2.ServletActionContext;
import org.hibernate.Session;
import tools.HibernateTools;
import tools.TimeTools;

import javax.persistence.*;
import java.sql.Timestamp;

@Entity
@Table(name = "userLog", schema = "bookloansystem", catalog = "bookloansystem")
public class UserLogEntity {
    private int id;
    private int UserId;
    private Timestamp timestamp;
    private String flag;
    private String ipAddress;//执行操作时的IP地址

    //禁止从内存直接拿到并修改系统日志---屏蔽所有set方法
    //本系统仅记录用户相关的行为--登录--注册--个人信息修改
    public UserLogEntity(){

    }
    private UserLogEntity(UserinfoEntity userinfoEntity, Object o,String flag,String ip){
        super();
        if (flag==null) {//兼容--当没有flag时再自动判断
            if (o instanceof Login) {
                UserId = userinfoEntity.getId();
                this.flag = Setting.UserLog_flag_UserLogin;
                timestamp = userinfoEntity.getLastLoginTime();
            } else if (o instanceof AddUser) {
                UserId = userinfoEntity.getId();
                this.flag = Setting.UserLog_flag_UserAdd;
                timestamp = userinfoEntity.getRegisteTime();
            } else if (o instanceof AltUser) {
                UserId = userinfoEntity.getId();
                this.flag = Setting.UserLog_flag_ManagerUpdateUserInfo;
                timestamp = userinfoEntity.getRegisteTime();
            }
        }
        else {//通用逻辑
            timestamp=new Timestamp(System.currentTimeMillis());
            this.flag=flag;
            this.UserId=userinfoEntity.getId();
        }
        this.ipAddress=ip;
        Session session= HibernateTools.getSession();
        session.save(this);
//        HibernateTools.commit(session);//交给调用者

        System.out.println("用户日志系统:  "+ userinfoEntity.getUsername()+"  " + TimeTools.timeStamp2Date(timestamp, null) +"  "+flag);

    }



    /**
     * 自动判断flag的日志记录
     * @param userinfoEntity
     * @param o
     * @return
     */
    public static boolean addLog(UserinfoEntity userinfoEntity,Object o){
        try {
            new UserLogEntity(userinfoEntity,o,null,ServletActionContext.getRequest().getLocalAddr());
        } catch (Exception e) {
            System.out.println("用户日志系统故障请检查");
            System.err.println(e.getMessage());
           return false;
        }

        return true;
    }

    /**
     * 指明操作名的日志记录
     * @param userinfoEntity
     * @param flag
     * @return
     */
    public static boolean addLog(UserinfoEntity userinfoEntity,String flag){
        try {
            new UserLogEntity(userinfoEntity,null,flag, ServletActionContext.getRequest().getLocalAddr());
        } catch (Exception e) {
            System.out.println("用户日志系统故障请检查");
            System.err.println(e.getMessage());
            return false;
        }

        return true;
    }
    public String BuildLog(){
        StringBuilder stringBuilder=new StringBuilder();
        Session session=HibernateTools.getSession();
        UserinfoEntity userinfoEntity=session.get(UserinfoEntity.class, this.UserId);
        stringBuilder.append(this.getTimestamp());
        if (userinfoEntity!=null)
            stringBuilder.append(userinfoEntity.getUsername());
        else
            stringBuilder.append("用户不存在,原ID为").append(this.UserId);
        stringBuilder.append("  ").append(this.getFlag());
        return stringBuilder.toString();
    }


    @Column(name = "id")
    @javax.persistence.Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public int getId() {
        return id;
    }
    private void setId(int id) {
        this.id = id;
    }
    @Basic
    @Column(name = "userid")
    public int getUserId() {
        return UserId;
    }

    private void setUserId(int userId) {
        UserId = userId;
    }
    @Basic
    @Column(name = "time")
    public Timestamp getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Timestamp timestamp) {
        this.timestamp = timestamp;
    }

    @Basic
    @Column(name = "IP")
    public String getIpAddress() {
        if (ipAddress==null||ipAddress.length()<7)
            return "未知";
        return ipAddress;
    }

    public void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
    }

    @Basic
    @Column(name = "flag")
    public String getFlag() {
        return flag;
    }

    private void setFlag(String flag) {
        this.flag = flag;
    }



}
